package com.yanqu.road.server.cmd.activity.realreward;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.player.UserActivityRealReward;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.RealRewardProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.ActivityRealRewardModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.List;

@Cmd(code = GameProtocol.S_COMMON_DRAW_GET_REAL_REWARD, desc = "")
public class ActivityDrawGetRealRewardCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        RealRewardProto.CommonDrawGetRealRewardReqMsg reqMsg = RealRewardProto.CommonDrawGetRealRewardReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();

        RealRewardProto.CommonDrawGetRealRewardRespMsg.Builder clientMsg = RealRewardProto.CommonDrawGetRealRewardRespMsg.newBuilder();
        clientMsg.setActivityId(activityId);

        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(ActivityMgr.activityInShowTime(activityInfo)){
            List<UserActivityRealReward> list = player.getModule(ActivityRealRewardModule.class).getUserRealRewardList(activityInfo);

            for (UserActivityRealReward data : list) {
                RealRewardProto.CommonDrawRealRewardMsg.Builder builder = RealRewardProto.CommonDrawRealRewardMsg.newBuilder();
                builder.setNo(data.getNo());
                builder.setGoodsId(data.getGoodsId());
                if (data.getAddrTime().getTime() != 0) {
                    builder.setPhone(data.getPhone());
                    builder.setAddr(data.getAddr());
                    builder.setSubmitTime(data.getAddrTime().getTime());
                }
                clientMsg.addRealReward(builder);
            }
        }
        player.sendPacket(ClientProtocol.U_COMMON_DRAW_GET_REAL_REWARD, clientMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
    }

}
